package com.vchello.controller.entrepren;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vchello.bean.entrepren.ProDelivery;
import com.vchello.bean.entrepren.ProDeliveryMilepost;
import com.vchello.bean.entrepren.ProDeliveryOrganization;
import com.vchello.bean.lib.Area;
import com.vchello.bean.lib.Field;
import com.vchello.bean.lib.Round;
import com.vchello.bean.user.User;
import com.vchello.controller.BaseController;
import com.vchello.service.entrepren.ProDeliveryMilepostService;
import com.vchello.service.entrepren.ProDeliveryOrganizationService;
import com.vchello.service.entrepren.ProDeliveryService;
import com.vchello.service.lib.AreaService;
import com.vchello.service.lib.FieldService;
import com.vchello.service.lib.RoundService;
import com.vchello.service.user.UserService;
import com.vchello.util.UploadFileHelper;
import com.vchello.util.Utility;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;  
/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/entrepren")
public class EntreprenController extends BaseController {
	
	private static final String REDIRECT_LOGIN = "redirect:/login";
	
	@Autowired
	private UserService userService;
	@Autowired
	private ProDeliveryService proDeliveryService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private RoundService roundService;
	@Autowired
	private FieldService fieldService;
	@Autowired
	private ProDeliveryOrganizationService proDeliveryOrganizationService;
	@Autowired
	private ProDeliveryMilepostService proDeliveryMilepostService;
	

	@RequestMapping(value = "/create/{userId}", method = RequestMethod.GET)
	public String create(Model model, @PathVariable int userId){
		User user = getUser();
		if(null == user) {
			return REDIRECT_LOGIN;
		}
		User userPro = userService.findByOne(userId);
		List<Area> areaLsit = areaService.findAll();
		List<Round> roundList = roundService.findAll();
		List<Field> fieldList = fieldService.findAll();
		ProDelivery proDelivery = proDeliveryService.findProDeliveryByUserId(user.getId());
		model.addAttribute("userPro", userPro).addAttribute("proDelivery", proDelivery).addAttribute("areaLsit", areaLsit).addAttribute("roundList", roundList).addAttribute("fieldList", fieldList);
		return "entrepren/index";
		
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public void create(String proDeliveryJson, String organizationJson, String milepostJson, int deliveryId) throws IOException{
		User user = getUser();
		if(null == user) {
			error("用户未登录");
			return;
		}
        JSONObject json1 = JSONObject.fromObject(proDeliveryJson);
        ProDelivery proDelivery = cretaeProDelivery(json1, user);
        if(null == proDelivery) {
        	error("项目基本信息为空");
        	return;
		}
       
        JSONArray jsonArray = JSONArray.fromObject(organizationJson); 
        List<ProDeliveryOrganization> list = (List<ProDeliveryOrganization>) JSONArray.toCollection(jsonArray, ProDeliveryOrganization.class);
        JSONArray milepostJsonArray = JSONArray.fromObject(milepostJson); 
        List<ProDeliveryMilepost> listMilepost = (List<ProDeliveryMilepost>) JSONArray.toCollection(milepostJsonArray, ProDeliveryMilepost.class);
        boolean isTrue = proDeliveryService.createDeliveryProject(proDelivery, list, listMilepost, user.getId(), deliveryId);
		if(isTrue) {
			success("投递项目成功");
			return;
		}
		error("投递项目失败");
	}
	
	@RequestMapping(value = "/deleteOrganization", method = RequestMethod.POST)
	public void deleteOrganization(int id){
		try {
			ProDeliveryOrganization entity = proDeliveryOrganizationService.findProDeliveryOrganizationById(id);
			if(null == entity){
				error("找不到要删除的团队信息");
				return;
			}
			proDeliveryOrganizationService.deleteProDeliveryOrganization(entity);
			success("删除团队信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			error("删除团队信息失败");
		}
	}
	
	@RequestMapping(value = "/deleteMilepost", method = RequestMethod.POST)
	public void deleteMilepost(int id){
		try {
			ProDeliveryMilepost entity = proDeliveryMilepostService.findProDeliveryMilepostById(id);
			if(null == entity){
				error("找不到要删除的里程牌信息");
				return;
			}
			proDeliveryMilepostService.deleteProDeliveryMilepost(entity);
			success("删除里程牌信息成功");
		} catch (Exception e) {
			e.printStackTrace();
			error("删除里程牌信息失败");
		}
	}
	
	private ProDelivery cretaeProDelivery(JSONObject json1, User user) throws IOException{
		ProDelivery proDelivery = (ProDelivery) JSONObject.toBean(json1, ProDelivery.class); 
        if(null == proDelivery) {
        	return null;
		}
		proDelivery.setCreateTime(Utility.getSimpleFormatedDayNow());
		proDelivery.setUserId(user.getId());
		String bpUrlStr = UploadFileHelper.isFileExists(proDelivery.getBp(), request);
		if (StringUtils.isNotBlank(bpUrlStr)) {
			proDelivery.setBp(bpUrlStr);
		}
		String logo = UploadFileHelper.isFileExists(proDelivery.getLogo(), request);
		if (StringUtils.isNotBlank(logo)) {
			proDelivery.setLogo(logo);
		}
		return proDelivery;
	}
}
